package com.smbms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smbms.dao.BillMapper;
import com.smbms.pojo.Bill;
import com.smbms.service.BillService;
import com.smbms.util.PageParams;
import com.smbms.util.PageResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;

/**
 * 类名：BillServiceImpl
 * 作者：小涛
 * 创建时间：2021/8/25-20:17
 * 功能：
 */
@Service
public class BillServiceImpl implements BillService {

    @Resource
    private BillMapper billMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public PageResult<Bill> findPage(PageParams pageParams) {
        //判断缓存
        String key = "bill:findPage:" + pageParams.getProductName() + ":" + pageParams.getProviderId() + ":" + pageParams.getIsPayment()
                + ":" + pageParams.getPage() + ":" + pageParams.getSize();
        if (redisTemplate.hasKey(key)) {
            return (PageResult<Bill>) redisTemplate.opsForValue().get(key);
        }

        PageHelper.startPage(pageParams.getPage(), pageParams.getSize());
        Page<Bill> page = billMapper.listParams(pageParams);

        PageResult<Bill> pageResult = new PageResult<>(page.getTotal(), page.getResult());
        redisTemplate.opsForValue().set(key, pageResult, Duration.ofHours(6));

        return pageResult;
    }

    @Override
    public int delBill(Integer id) {
        return billMapper.delBill(id);

    }

    @Override
    public int checkBillCode(Bill bill) {
        return billMapper.checkBillCode(bill);
    }

    @Override
    public int addBill(Bill bill) {
        return billMapper.addBill(bill);
    }

    @Override
    public Bill findById(Integer id) {
        return billMapper.findById(id);
    }

    @Override
    public int checkBillCodeUpd(String billCode, String self) {
        return billMapper.checkBillCodeUpd(billCode,self);
    }

    @Override
    public int updBill(Bill bill) {
        return billMapper.updBill(bill);
    }
}
